package com.kkd.kkdapp.activity;

import java.util.List;

import com.google.gson.reflect.TypeToken;
import com.kkd.kkdapp.R;
import com.kkd.kkdapp.adapter.RepaymentScheduleAdapter;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.entity.RepaymentSchedule;
import com.kkd.kkdapp.http.entity.RepaymentScheduleData;
import com.kkd.kkdapp.http.manager.HttpCallBack;

import com.kkd.kkdapp.http.manager.KkdHttpMagager;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 还款计划
 * 
 * @author mty
 * 
 */
public class RepaymentScheduleActivity extends BaseActivity implements
		OnClickListener {

	private final int CODE_ZERO = 1; // code=0
	private final int CODE_ELSE = 2; // code= 其它
	private RelativeLayout schedule_top_img; // 返回键
	private ListView mListView;
	private RepaymentScheduleAdapter mAdapter;
	private List<RepaymentScheduleData> dataList;
	private ImageView img_null;
	private TextView tv_null;

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case CODE_ZERO:
				mAdapter.appendData(dataList, false);
				mAdapter.update();
				if (dataList != null) {
					if (dataList.size() > 0) {
						img_null.setVisibility(View.GONE);
						tv_null.setVisibility(View.GONE);
					} else {
						img_null.setVisibility(View.VISIBLE);
						tv_null.setVisibility(View.VISIBLE);
					}
				}
				break;
			case CODE_ELSE:
				String desc = (String) msg.obj;
				showToast(desc);
				break;
			default:
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_repayment_schedule);
		initView();
		setListener();
		getMessage();
	}

	public void initView() {
		schedule_top_img = (RelativeLayout) this
				.findViewById(R.id.schedule_top_img);
		mListView = (ListView) this.findViewById(R.id.mylistview);
		img_null = (ImageView) this.findViewById(R.id.img_null);
		tv_null = (TextView) this.findViewById(R.id.tv_null);
		mAdapter = new RepaymentScheduleAdapter(this);
		mListView.setAdapter(mAdapter);
	}

	public void setListener() {
		schedule_top_img.setOnClickListener(this);
	}

	public void getMessage() {
		Intent intent = this.getIntent();
		String bidId = intent.getStringExtra("bidId");
		requestRepayment(bidId);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.schedule_top_img:
			finish();
			break;
		}
	}

	public void requestRepayment(String bid_id) {
		KkdHttpMagager.getInstance().getRepaymentScheduleManager(bid_id,
				new TypeToken<List<RepaymentScheduleData>>() {
				}.getType(), new HttpCallBack() {

					@SuppressWarnings("unchecked")
					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						dataList = (List<RepaymentScheduleData>) responseBody;
						mHandler.sendEmptyMessage(CODE_ZERO);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						Message msg = new Message();
						msg.what = CODE_ELSE;
						msg.obj = desc;
						mHandler.sendMessage(msg);
					}
				});

		// RepaymentScheduleManager mRepaymentScheduleManager = new
		// RepaymentScheduleManager(
		// this);
		// mRepaymentScheduleManager.getRepaymentScheduleManager(bid_id,
		// new AsyncHttpResponseHandler() {
		//
		// @Override
		// public void onSuccess(int statusCode, Header[] headers,
		// byte[] responseBody) {
		// String responseString = new String(responseBody);
		// LogUtil.e("RepaymentScheduleActivity_responseString",
		// responseString);
		// RepaymentScheduleParser mRepaymentScheduleParser = new
		// RepaymentScheduleParser();
		// RepaymentSchedule mRepaymentSchedule = mRepaymentScheduleParser
		// .getRepaymentScheduleParser(responseString);
		// if (mRepaymentSchedule.getCode() == 0) {
		// dataList = mRepaymentSchedule.getData();
		// mAdapter.appendData(dataList, false);
		// mAdapter.update();
		// if (dataList != null) {
		// if (dataList.size() > 0) {
		// img_null.setVisibility(View.GONE);
		// tv_null.setVisibility(View.GONE);
		// } else {
		// img_null.setVisibility(View.VISIBLE);
		// tv_null.setVisibility(View.VISIBLE);
		// }
		// }
		// } else {
		// Toasttool.MyToast(RepaymentScheduleActivity.this,
		// mRepaymentSchedule.getDesc());
		// }
		//
		// }
		//
		// @Override
		// public void onFailure(int statusCode, Header[] headers,
		// byte[] responseBody, Throwable error) {
		//
		// }
		// });
	}
}
